package net.wyxj.transfer.tools;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import net.wyxj.transfer.Constant;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

public class ConfigTools {

	public static Map<String, String> readStoreInfo() {
		Gson gson = new Gson();
		String settings = null;
		try {
			settings = new String(FileUtils.readByteArray(
					Constant.CONFIGURATION_FILE_NAME),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (settings != null) {
			try {
				@SuppressWarnings("unchecked")
				Map<String, String> info = gson.fromJson(settings,
						HashMap.class);
				return info;
			} catch (JsonSyntaxException e) {
				return null;
			}
		}
		return null;
	}

	public static void writeStoreInfo(Map<String, String> info) {
		Gson gson = new Gson();
		String settings = gson.toJson(info);
		try {
			FileUtils.writeToFile(Constant.CONFIGURATION_FILE_NAME,
					settings.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
